package com.literature.search.file.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.literature.search.core.utils.sp.SpUserUtil;
import com.literature.search.file.entity.dto.SearchAllDTO;
import com.literature.search.file.entity.dto.SearchSingleDTO;
import com.literature.search.file.service.IElasticService;
import com.literature.search.file.service.RedisService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName RedisAspect
 * @Description TODO
 * @Author abin
 * @Date 2023/3/6 17:11
 * @Version 1.0
 */
@Aspect
@Component
public class RedisAspect {

    @Resource
    private RedisService redisService;

    @Resource
    private IElasticService elasticService;

    //定义浏览文件的切面
    @Pointcut("execution(public * com.literature.search.file.controller.FileController.serveFileOnline(..))")
    public void viewPointCut(){}

    //定义下载文件的切面
    @Pointcut("execution(public * com.literature.search.file.controller.FileController.downloadFileById(..))")
    public void downloadPointCut(){}

    //定义删除文件的切面
    @Pointcut("execution(public * com.literature.search.file.controller.FileController.deleteFileByGetMethod(..))")
    public void deleteFilePointCut(){}

    //定义关键字搜索的切面
    @Pointcut("execution(public * com.literature.search.file.controller.ElasticController.searchAll(..))")
    public void searchAllPointCut(){}

    @Pointcut("execution(public * com.literature.search.file.controller.ElasticController.searchSingle(..))")
    public void searchSinglePointCut(){}


    //定义点赞收藏的切面
    @Pointcut("execution(public * com.literature.search.file.controller.LikeAndCollectionController.like(..)) " +
            "|| execution(public * com.literature.search.file.controller.LikeAndCollectionController.collect(..))")
    public void likeAndCollectPointCut(){}

    //定义取消点赞和取消收藏的切面
    @Pointcut("execution(public * com.literature.search.file.controller.LikeAndCollectionController.dislike(..))"+
            "|| execution(public * com.literature.search.file.controller.LikeAndCollectionController.cancelCollect(..))")
    public void dislikeAndCancelCollectPointCut(){}

    //在浏览文件方法执行后
    @After("viewPointCut()")
    public void viewAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        String fileId = (String) objs[0];
        //根据文件id更新浏览量
        redisService.countFileViews(fileId);
        redisService.insertNeededUpdateFileId(fileId);
        redisService.countHotDocument(fileId,1);
    }

    //在删除文件方法执行后
    @After("deleteFilePointCut()")
    public void deleteFileAfter(JoinPoint joinPoint){
        Object[] objs = joinPoint.getArgs();
        String fileId = (String) objs[0];
        //根据文件id删除浏览量记录和下载量记录
        redisService.deleteFileView(fileId);
        redisService.deleteFileDownload(fileId);
    }


    //在下载方法执行后
    @After("downloadPointCut()")
    public void downloadAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        String fileId = (String) objs[0];
        //根据文件id更新下载量
        redisService.countFileDownloads(fileId);
        redisService.insertNeededUpdateFileId(fileId);
        redisService.countHotDocument(fileId,1);
    }

    //在执行搜索方法后
    @After("searchAllPointCut()")
    public void searchAllAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        SearchAllDTO searchDTO = (SearchAllDTO) objs[0];
        String value = searchDTO.getKeyword();
        redisService.insertSearchHistory(SpUserUtil.getCurrUser().getId(), value);
        redisService.countSearchHotWord(value);
    }

    @After("searchSinglePointCut()")
    public void searchSingleAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        SearchSingleDTO searchSingleDTO = (SearchSingleDTO) objs[0];
        String fileName = searchSingleDTO.getFileName();
        fileName = fileName.substring(0, fileName.lastIndexOf("."));    //排除文件名的后缀
        //System.out.println("fileName = " + fileName);
        String userId = StpUtil.getLoginIdAsString();
        Integer originHistoryKeywordsCount = redisService.getDocumentHistoryKeywordCount(userId);
        //System.out.println("originHistoryKeywordsCount = " + originHistoryKeywordsCount);
        int newHistoryKeywordsCount = elasticService.insertHistoryKeywords(userId,fileName,originHistoryKeywordsCount);
        //System.out.println("newHistoryKeywordsCount = " + newHistoryKeywordsCount);
        redisService.setDocumentHistoryKeywordCount(userId, newHistoryKeywordsCount);
    }

    //在执行点赞收藏方法后
    @After("likeAndCollectPointCut()")
    public void likeAndCollectAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        String fileId = (String) objs[0];
        redisService.insertNeededUpdateFileId(fileId);
        redisService.countHotDocument(fileId,1);
    }

    //在执行取消点赞收藏方法后
    @After("dislikeAndCancelCollectPointCut()")
    public void dislikeAndCancelCollectAfter(JoinPoint joinPoint) {
        Object[] objs = joinPoint.getArgs();
        String fileId = (String) objs[0];
        redisService.insertNeededUpdateFileId(fileId);
        redisService.countHotDocument(fileId,-1);
    }
}
